package com.mark.service.impl;


import com.mark.entity.school.UTeacher;
import com.mark.entity.school.UTeacherGroupDetail;
import com.mark.mapper.UTeacherGroupDetailMapper;
import com.mark.mapper.UTeacherGroupMapper;
import com.mark.entity.school.UTeacherGroup;
import com.mark.service.UTeacherGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * (UTeacherGroup)表服务实现类
 *
 * @author Deng PengFei
 * @since 2021-10-27 11:18:25
 */
@Service
public class UTeacherGroupServiceImpl implements UTeacherGroupService {
    @Autowired
    private UTeacherGroupMapper uTeacherGroupMapper;
    @Autowired
    private UTeacherGroupDetailMapper uTeacherGroupDetailMapper;


    /**
     * 获取教师分组列表
     *
     * @return 对象列表
     */
    @Override
    public List<UTeacherGroup> getTeaGroupList() {
        return this.uTeacherGroupMapper.getTeaGroupList();
    }


    /**
     * 增加一个教师分组
     *
     * @param uTeacherGroup 分组对象
     * @param teachers      分组成员
     * @return 1001 成功， 1002 分组已经存在 . 1003 失败
     */
    @Transactional
    @Override
    public Integer addTeaGro(UTeacherGroup uTeacherGroup, List<UTeacher> teachers) {
        //查询分组是否存在
        UTeacherGroup newTeaGro = new UTeacherGroup();
        newTeaGro.setGroNm(uTeacherGroup.getGroNm());
        List<UTeacherGroup> groups = this.uTeacherGroupMapper.queryAllByCondition(newTeaGro);
        if (groups.size() > 0) {  // 分组已经存在
            return 1002;
        }
        // 往 分组表插入一条记录 获取返回的主键 groId
        this.uTeacherGroupMapper.insert(uTeacherGroup);
        int groId = uTeacherGroup.getGroId();  // 获取返回的主键 groId
        // 往 教师组详情插入一条数据
        UTeacherGroupDetail uTeacherGroupDetail = new UTeacherGroupDetail();
        for (UTeacher teacher : teachers) {
            uTeacherGroupDetail.setGroId(groId);
            uTeacherGroupDetail.setGroNm(uTeacherGroup.getGroNm());
            uTeacherGroupDetail.setTeaId(teacher.getTeaId());
            uTeacherGroupDetail.setTeaNm(teacher.getTeaNm());
            uTeacherGroupDetail.setDetDesc(uTeacherGroup.getGroDesc());
            this.uTeacherGroupDetailMapper.insert(uTeacherGroupDetail);
        }
        return 1001;
    }
}